package com.sgb.kjwl.view.ui.fragment.shop;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentGroupBuyBinding;
import com.sgb.kjwl.di.shopping.DaggerShoppingFragComponent;
import com.sgb.kjwl.di.shopping.ShoppingFragModule;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.viewmodel.shopping.ShopGroupBuyCategoryModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 集采二级界面
 */
public class ShopGroupBuyCategoryFragment extends AppLazyFragment {

    private static final String KEY_INDEX = "index";
    private static final String KEY_TYPE = "item_type";
    private static final String KEY_CENTRALIZED = "centralized";
    private static final String KEY_KEYWORDS = "searchWords";

    @Inject
    ShopGroupBuyCategoryModel mModel;
    private FragmentGroupBuyBinding mBind;

    private int firstIndex;
    private int mCentralized;
    private String mGroupBuyType = "1";

    private boolean isFirstLoadData = true;

    private String keywords = "";

    /**
     * @param position
     * @param type     “ ”集采大厅  1 即报集采  2 汇总集采 3 企业专项
     * @return
     */
    public static ShopGroupBuyCategoryFragment newInstance(int index, int position, String type, String keywords) {
        ShopGroupBuyCategoryFragment fragment = new ShopGroupBuyCategoryFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_INDEX, index);
        bundle.putInt(KEY_CENTRALIZED, position);
        bundle.putString(KEY_TYPE, type);
        bundle.putString(KEY_KEYWORDS, keywords);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerShoppingFragComponent.builder()
                .appComponent(appComponent)
                .shoppingFragModule(new ShoppingFragModule(ShopGroupBuyCategoryFragment.this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBind = DataBindingUtil.inflate(inflater, R.layout.fragment_group_buy, container, false);
        return mBind.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getArguments() != null) {
            firstIndex = getArguments().getInt(KEY_INDEX);
            mCentralized = (int) getArguments().getInt(KEY_CENTRALIZED);
            mGroupBuyType = getArguments().getString(KEY_TYPE);
            keywords = getArguments().getString(KEY_KEYWORDS);

        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    protected void onLazyLoad() {
        if (isFirstLoadData) {
            // 集采分类 1商品集采 2 机械集采 3 设备集采 4 劳务集采 5房产集采 6租赁集采
            mModel.getFirstCategory(mCentralized);
            isFirstLoadData = false;
        }
        if (mCentralized == 0) {//手动添加的全部
            mBind.tabLayout.setVisibility(View.GONE);
        } else {
            mBind.tabLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onVisible() {
        super.onVisible();
    }

    public void showFirstCategory(List<PickSelectEntity> data) {
        mBind.emptyView.setVisibility(View.GONE);
        if (data == null || data.size() == 0) {
            DialogHelper.dismissProgressDialog();
            showEmpty();
            return;
        }
        ArrayList<Fragment> mFragments = new ArrayList<>();
        int size = data.size();
        String[] titles = new String[size];
        for (int i = 0; i < titles.length; i++) {//一级分类：钢材、钢筋、水泥………………
            mFragments.add(ShopGroupBuyChildFragment.newInstance(data.get(i).getNo(), mGroupBuyType, mCentralized, keywords));
            titles[i] = data.get(i).getProjectscale();
        }
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getChildFragmentManager(), mFragments, titles);
        mBind.viewPager.setAdapter(mPagerAdapter);
        mBind.viewPager.setOffscreenPageLimit(titles.length);
//        mBind.viewPager.setOffscreenPageLimit(1);
        mBind.tabLayout.setupWithViewPager(mBind.viewPager);
    }

    public void showEmpty() {
        mBind.emptyView.setVisibility(View.VISIBLE);
    }
}
